import { defineStore } from 'pinia'

export const useTabStore = defineStore('tab', {
    state: () => ({
        tabs: [],
        activeTab: ''
    }),
    actions: {
        addTab(menuItem) {
            const exists = this.tabs.some(tab => tab.key === menuItem.menu.path)
            if (!exists) {
                this.tabs.push({
                    key: menuItem.menu.path,
                    title: menuItem.menu.menuName,
                    closable: true
                })
            }
            this.activeTab = menuItem.menu.path
        },
        closeTab(key) {
            this.tabs = this.tabs.filter(tab => tab.key !== key)
            if (this.activeTab === key && this.tabs.length > 0) {
                this.activeTab = this.tabs[this.tabs.length - 1].key
            }
        },
        clean(){
            this.tabs = []
        }
    },
    persist: true // 启用持久化存储（需要已安装持久化插件）
})